package com.notebad;

import java.util.Observable;
import java.util.Observer;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

import com.notebad.note.Note;
import com.notebad.tools.GPSLocationTracker;
import com.notebad.ui.GeolocalisationView;

public class TestGeolocalisation extends Activity implements OnClickListener, Observer {

	private static final String TAG = "TestGeolocalisation";
	private Note note;
	private TextView mLong;
	private TextView mLat;
	private Button mButton;
	private GPSLocationTracker tracker;

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.testgeolocalisation);
		note = Note.getCurrentNote();
		mLong = (TextView) findViewById(R.id.textlong);
		mLat = (TextView) findViewById(R.id.textlat);
		mButton = (Button) findViewById(R.id.button);
		mButton.setOnClickListener(this);
		
		tracker = new GPSLocationTracker(this);    
		tracker.addObserver(this);
		tracker.start();
	}	

	@Override
	public void onClick(View v) {

		Intent i;
		switch (v.getId()) {
		case R.id.button:
			Log.d(TAG, "onClick - button ");
			i = new Intent(this, GeolocalisationView.class);
			Log.d(TAG, "latitude : " + note.getLatitude());
			Log.d(TAG, "longitude : " + note.getLongitude());
			startActivity(i);
			break;
		}		
	}
	
	@Override
	protected void onStop() {
		super.onStop();
		Log.d(TAG, "onStop");
		try {
			Note.setCurrentNote(note);
			finalize();
		} catch (Throwable e) {
			Log.e(TAG,e.getMessage());
		}
		finish();
	}

	@Override
	public void update(Observable observable, Object data) {
		Log.d(TAG, "update");
		note.setGeoPoint(tracker.getGeoPoint());
		note.setAddress(tracker.getAddress());	
		mLat.setText(""+note.getLatitude());
		mLong.setText(""+note.getLongitude());
	}
}